/*
 * hmc5883_support.cpp
 *
 *  Created on: Feb 28, 2013
 *      Author: Kurt Hardin
 */

#include "hmc5883_support.h"
#include "i2c_support.h"

/****************************************************************************
 * Library functions for reading/writing from/to an HMC5883 magnetometer.	*
 ****************************************************************************/

/*
 * Read data for all axes from an HMC5883 magnetometer.
 * Returns: Magnetometer data
 ************************************************************************/
void hmc5883_read_all(MagData * data)
{
    uint8 tmp_data[6];
	mcf52233_i2c_read(HMC5883_BASE_ADDR, HMC5883_REG_X_HSB, tmp_data, 6);
	
    data->setXMSB(tmp_data[0]);
    data->setXLSB(tmp_data[1]);
    data->setZMSB(tmp_data[2]);
    data->setZLSB(tmp_data[3]);
    data->setYMSB(tmp_data[4]);
    data->setYLSB(tmp_data[5]);
}

/*
 * Read data for a single axis from an HMC5883 magnetometer
 * Returns: Single axis magnetometer data
 */
int16 hmc5883_read_axis(uint8 reg_addr)
{
    uint8 tmp_data[2];
    mcf52233_i2c_read(HMC5883_BASE_ADDR, reg_addr, tmp_data, 2);
    return (int16) ((tmp_data[0] << 8) | tmp_data[1]);
}

/*
 * Write to an HMC5883 register
 * Returns: nothing
 */
void hmc5883_write(uint8 reg_addr, uint8 data)
{
	mcf52233_i2c_write(HMC5883_BASE_ADDR, reg_addr, data);
}
